package com.novanok.micurso;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.novanok.micurso.entidad.Alumno;
import com.novanok.micurso.entidad.Comun;
import com.novanok.micurso.entidad.Profesor;

import java.util.Map;

public class MyActivity extends Activity {

    private static final String TAG = "MyActivity";
    private ProgressBar spinner;
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-8819200492193671/2774149546");
        adView.setAdSize(AdSize.BANNER);
        LinearLayout layout = (LinearLayout) findViewById(R.id.banner1);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        spinner = (ProgressBar)findViewById( R.id.progressBar1 );
        spinner.setVisibility( View.VISIBLE );
        findViewById( R.id.panelMenu ).setVisibility( View.GONE );

        Firebase.setAndroidContext(this);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        String modo = sharedPref.getString("modo", "");

        Log.d(TAG, modo);

        if ( modo.compareTo("") != 0 ){
            if ( modo.compareTo("alumno") == 0 ){
                this.alumno(null);
            } else {
                this.profesor(null);
            }
        } else {
            spinner.setVisibility(View.GONE);
            findViewById(R.id.panelMenu).setVisibility(View.VISIBLE);
        }
    }

    public void profesor(View v){
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPref.edit().putString("modo", "profesor").apply();

        final String profesor = sharedPref.getString("profesor_id", "");

        if ( profesor.compareTo("") == 0 ){
            Intent i = new Intent(MyActivity.this, ProfesorActivity.class);
            startActivity(i);
        } else {
            Firebase rootRef = new Firebase("https://mi-curso.firebaseio.com/profesor/" + profesor);
            rootRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Map<String, String> newPost = (Map<String, String>) dataSnapshot.getValue();
                    Profesor p = new Profesor();
                    p.setProfesor_id( profesor );
                    p.setNombre(newPost.get("nombre").toString());
                    p.setEmail(newPost.get("email").toString());
                    p.cargarMisCursos();

                    Comun.setProfesor(p);
                    Intent i = new Intent(MyActivity.this, CursosProfesorActivity.class);
                    startActivity(i);

                    MyActivity.this.finish();
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }
    }

    public void alumno(View v){
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPref.edit().putString("modo", "alumno").apply();

        final String alumno = sharedPref.getString("alumno_id", "");

        Log.d("TAG", alumno);

        if ( alumno.compareTo("") == 0 ){
            Intent i = new Intent(MyActivity.this, AlumnoActivity.class);
            startActivity(i);
        } else {
            Firebase rootRef = new Firebase("https://mi-curso.firebaseio.com/alumno/" + alumno);
            rootRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Map<String, String> newPost = (Map<String, String>) dataSnapshot.getValue();
                    Alumno a = new Alumno();
                    a.setId(alumno);
                    a.setNombre(newPost.get("nombre").toString());
                    a.setEmail(newPost.get("email").toString());
                    a.setLegajo(newPost.get("legajo").toString());
                    a.cargarMisCursos();

                    Comun.setAlumno(a);

                    Intent i = new Intent(MyActivity.this, CursosAlumnoActivity.class);
                    startActivity(i);

                    MyActivity.this.finish();
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }
    }
}
